/** 
 *
 * Lisense:
 * Copyright (c) 2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @fileDesc:TODO
 * @author:dairymix
 * @version:1.0.0
 */
package cn.wanghaomiao.provider;

import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BestMatchSpecFactory;
import org.apache.http.impl.cookie.BrowserCompatSpecFactory;

/** 
 * @ClassName:MyCookieReg
 * @Description:TODO
 * @author:dairymix
 * @version:1.0.0
 */
public class MyCookieReg {

    public MyCookieReg() {
        regCookie();
    }

    CookieSpecProvider easySpecProvider;
    Registry<CookieSpecProvider> reg;
    RequestConfig requestConfig;
    CloseableHttpClient httpclient;

    public CookieSpecProvider getEasySpecProvider() {
        return easySpecProvider;
    }

    public void setEasySpecProvider(CookieSpecProvider easySpecProvider) {
        this.easySpecProvider = easySpecProvider;
    }

    public Registry<CookieSpecProvider> getReg() {
        return reg;
    }

    public void setReg(Registry<CookieSpecProvider> reg) {
        this.reg = reg;
    }

    public RequestConfig getRequestConfig() {
        return requestConfig;
    }

    public void setRequestConfig(RequestConfig requestConfig) {
        this.requestConfig = requestConfig;
    }

    public CloseableHttpClient getHttpclient() {
        return httpclient;
    }

    public void setHttpclient(CloseableHttpClient httpclient) {
        this.httpclient = httpclient;
    }

    public void regCookie() {
        setEasySpecProvider(new MyCookieSpecProvider());
        setReg(RegistryBuilder
                .<CookieSpecProvider> create()
                .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
                .register(CookieSpecs.BROWSER_COMPATIBILITY,new BrowserCompatSpecFactory())
                .register("mySpec", easySpecProvider).build());
        setRequestConfig(RequestConfig.custom().setCookieSpec("mySpec").build());
        setHttpclient(HttpClients
                .custom()
                .setDefaultCookieSpecRegistry(getReg())
                .setDefaultRequestConfig(getRequestConfig())
                .build());
    }

}
